package com.my.fo.strategy.organization.type;

import cn.hutool.core.io.FileUtil;
import com.my.fo.strategy.interfaces.StrategyFactoryName;
import com.my.fo.strategy.organization.inter.OrganizationExistTypeStrategy;
import com.my.fo.strategy.organization.inter.OrganizationTypeStrategy;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Files;

@StrategyFactoryName("softLink")
@Slf4j
public class SoftLinkOrganizationTypeStrategy implements OrganizationTypeStrategy {
    @Override
    public boolean handlerType(String inputSrc, String outputSrc, OrganizationExistTypeStrategy existTypeStrategy) {
        return false;
    }

    @Override
    public boolean handlerType(File inputFile, File outputFile, OrganizationExistTypeStrategy existTypeStrategy) {
        try {
            if (existTypeStrategy.handlerExist(inputFile, outputFile)) {
                if (FileUtil.exist(outputFile)) FileUtil.del(outputFile);
                Files.createSymbolicLink(outputFile.toPath(), inputFile.toPath());
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new RuntimeException("【软连接】文件整理发生错误：", e);
        }
    }
}
